/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.softwares.web;

import java.util.Date;
import java.util.Set;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.model.User;
import org.crystalrange.service.UserService;
import org.crystalrange.softwares.model.Software;
import org.crystalrange.softwares.model.SoftwareVersion;
import org.crystalrange.softwares.service.SoftwareService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 * 
 * @author Krzysiek
 */
public class SoftwareController extends BaseController {

	private Software software;
	private SoftwareService softwareService;
	protected Listbox listboxSoftwareVersion;
	private SoftwareVersion selectedSoftwareVersion;
	private UserService userService;

	public Software getSoftware() {
		return software;
	}

	public void setSoftware(Software software) {
		this.software = software;
	}

	public SoftwareService getSoftwareService() {
		return softwareService;
	}

	public void setSoftwareService(SoftwareService softwareService) {
		this.softwareService = softwareService;
	}

	public SoftwareVersion getSelectedSoftwareVersion() {
		return selectedSoftwareVersion;
	}

	public void setSelectedSoftwareVersion(
			SoftwareVersion selectedSoftwareVersion) {
		this.selectedSoftwareVersion = selectedSoftwareVersion;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void onClick$toolbarbuttonSoftwareSave(Event event) throws Exception {
		this.softwareSave();
	}

	public void onClick$menuitemSoftwareSave(Event event) throws Exception {
		this.softwareSave();
	}

	public void onClick$menuitemSoftwareVersionCreate(Event event)
			throws Exception {
		this.softwareVersionCreate();
	}

	public void onClick$menuitemSoftwareVersionEdit(Event event)
			throws Exception {
		this.softwareVersionEdit();
	}
	
	public void onClick$menuitemSoftwareVersionDelete(Event event)
			throws Exception {
		this.softwareVersionDelete();
	}
	
	public void onClick$toolbarbuttonSoftwareVersionCreate(Event event)
			throws Exception {
		this.softwareVersionCreate();
	}

	public void onClick$toolbarbuttonSoftwareVersionEdit(Event event)
			throws Exception {
		this.softwareVersionEdit();
	}
	
	public void onClick$toolbarbuttonSoftwareVersionDelete(Event event)
			throws Exception {
		this.softwareVersionDelete();
	}

	private void softwareSave() throws Exception {
		if (this.software.getName() != null) {
			this.softwareService.save(this.software);
			this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
			Clients.evalJavaScript("window.history.back();");
		} else {
			this.message.setText("You must enter 'Software Name'.");
            this.message.setIcon("/img/exclamation.png");
            this.binder.loadAll();
		}
	}

	private void softwareVersionCreate() throws Exception {

		String username = SecurityContextHolder.getContext()
				.getAuthentication().getName();
		User currentUser = this.userService.findByUsername(username);
		
		SoftwareVersion softwareVersion = new SoftwareVersion();
		softwareVersion.setSoftware(this.software);
		softwareVersion.setCreatedBy(currentUser);
		softwareVersion.setCreatedDate(new Date());
		this.execution.setAttribute("softwareVersion", softwareVersion);

		Window window = (Window) this.execution.createComponents(
				"~./zul/html/softwareVersion.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			this.software.getSoftwareVersions().add(
					(SoftwareVersion) this.execution
							.getAttribute("softwareVersion"));
		}

		this.binder.loadAll();
	}

	private void softwareVersionEdit() throws Exception {
		this.execution.setAttribute("softwareVersion",
				this.selectedSoftwareVersion);

		Window window = (Window) this.execution.createComponents(
				"~./zul/html/softwareVersion.zul", null, null);
		window.setParent(self);
		window.doModal();

		this.binder.loadAll();
	}
	
	private void softwareVersionDelete() throws Exception {
		Set<Listitem> selectedItems = this.listboxSoftwareVersion.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    SoftwareVersion softwareVersion = (SoftwareVersion) selectedItem.getValue();
                    this.software.getSoftwareVersions().remove(softwareVersion);
                }
                this.selectedSoftwareVersion = null;
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.binder.loadAll();
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);

		if (this.execution.getParameter("Id") != null) {
			this.software = this.softwareService.find(Long
					.parseLong(this.execution.getParameter("Id")));
			this.labelTitle.setValue("Edit Software");
		} else {
			this.software = this.softwareService.create(SecurityContextHolder
					.getContext().getAuthentication().getName());
			this.labelTitle.setValue("Create Software");
		}

		component.setAttribute("controller", this);
	}
}
